﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using HotelManager.Model;
using HotelManager.Core;

namespace HotelManager.DataAccess
{
    class ThuePhongAccess
    {
        dbConnection db = new dbConnection();
        public DataTable laytoanbophongthue()
        {
            return db.executeSelectProcedureQueryNonParameter("selectThuePhong");
        }

        // Tìm kiếm phòng thuê theo mã số
        public DataView timkiemMasophong(string maso)
        {
            DataView dataView = laytoanbophongthue().DefaultView;
            dataView.RowFilter = string.Format("MaSo like '%{0}%'", maso);
            return dataView;
        }

        // tìm kiếm theo mã phòng
        public DataView timkiemMaphong(string maphong)
        {
            DataView dataView = laytoanbophongthue().DefaultView;
            dataView.RowFilter = string.Format("MaPhong like '%{0}%'", maphong);
            return dataView;
        }

        // tìm kiếm phòng theo mã số và trả về 1 bảng
        public DataTable timPhongtheomaso(string maso)
        {
            SqlParameter[] listParam = new SqlParameter[1];
            SqlParameter ma = new SqlParameter("@MaSo", SqlDbType.Char, 10);
            ma.Value = maso;
            listParam[0] = ma;
            return db.executeSelectProcedureQuery("timkiemMSPhong", listParam);
        }

        public int suaThuePhong(ThuePhong thuephong)
        {
            if ((timPhongtheomaso(thuephong._MaSo) == null))
                return 0;
            else
            {
                SqlParameter[] listParam = new SqlParameter[9];
                SqlParameter maso = new SqlParameter("@MaSo", SqlDbType.Char, 10);
                maso.Value = thuephong._MaSo;
                listParam[0] = maso;
                listParam[1] = new SqlParameter("@MaPhong", thuephong._MaPhong);
                listParam[2] = new SqlParameter("@NgayNhanPhong",thuephong._NgayNhan);
                listParam[3] = new SqlParameter("@NgayTraPhong", thuephong._NgayTra);
                listParam[4] = new SqlParameter("@MaKhach", thuephong._MaKhach);
                listParam[5] = new SqlParameter("@SoLuongKhach", thuephong._SoLuongKH);
                listParam[6] = new SqlParameter("@TienDatCoc", thuephong._TienDatCoc);
                listParam[7] = new SqlParameter("@TongTra", thuephong._TongTra);
                listParam[8] = new SqlParameter("@MaNV", thuephong._MaNV);
                db.executeUpdateProcedureQuery("updateThuePhong", listParam);
                return 1;
            }

        }

        public int themThuePhong(ThuePhong thuephong)
        {
            if ((timPhongtheomaso(thuephong._MaSo).Rows.Count > 0))
                return 0;
            else
            {
                SqlParameter[] listParam = new SqlParameter[9];
                SqlParameter maso = new SqlParameter("@MaSo", SqlDbType.Char, 10);
                maso.Value = thuephong._MaSo;
                listParam[0] = maso;
                listParam[1] = new SqlParameter("@MaPhong", thuephong._MaPhong);
                listParam[2] = new SqlParameter("@NgayNhanPhong", thuephong._NgayNhan);
                listParam[3] = new SqlParameter("@NgayTraPhong", thuephong._NgayTra);
                listParam[4] = new SqlParameter("@MaKhach", thuephong._MaKhach);
                listParam[5] = new SqlParameter("@SoLuongKhach", thuephong._SoLuongKH);
                listParam[6] = new SqlParameter("@TienDatCoc", thuephong._TienDatCoc);
                listParam[7] = new SqlParameter("@TongTra", thuephong._TongTra);
                listParam[8] = new SqlParameter("@MaNV", thuephong._MaNV);
                db.executeInsertProcedureQuery("insertThuePhong", listParam);
                return 1;
            }
        }

        public int xoaThuePhong(ThuePhong thuephong)
        {
            if ((timPhongtheomaso(thuephong._MaSo) == null))
                return 0;
            else
            {
                SqlParameter[] listParam = new SqlParameter[1];
                listParam[0] = new SqlParameter("@MaSo", thuephong._MaSo);
                db.executeDeleteProcedureQuery("deleteThuePhong", listParam);
                return 1;
            }
        }
    }
}

